package backtrack;

import java.util.ArrayList;
import java.util.List;

/**
 * @author : Rookie
 * @description : 全排列
 * @date : 2023/12/13
 */
public class FullPermutation {

    public static void main(String[] args) {
        int[] nums = new int[] {1,2,4};
        boolean[] used = new boolean[nums.length];
        List<List<Integer>> result = new ArrayList<>();
        backtrack(nums,used,new ArrayList<>(),result);
        System.out.println(result);
    }

    private static void backtrack(int[] nums, boolean[] used, List<Integer> path,List<List<Integer>> result) {
        if (path.size() == nums.length) {
            result.add(new ArrayList<>(path));
            return;
        }

        for (int i = 0; i < nums.length; i++) {
            if (used[i]) {
                continue;
            }
            path.add(nums[i]);
            used[i] = true;
            backtrack(nums, used, path, result);
            path.remove(path.size()-1);
            used[i] = false;
        }
    }
}
